package cn.tedu.ticket;
/**本类用于解决实现继承Thread方式的售票安全隐患问题*/
public class TicketThreadV2 {
    public static void main(String[] args) {
        TicketTV2 t1=new TicketTV2();
        TicketTV2 t2=new TicketTV2();
        TicketTV2 t3=new TicketTV2();
        TicketTV2 t4=new TicketTV2();
        t1.start();
        t2.start();
        t3.start();
        t4.start();
    }
}
class TicketTV2 extends Thread{
    static int tickets=100;
    //static Object o=new Object();
    @Override
    public void run() {
        while (true){
            synchronized (TicketTV2.class){//
                if (tickets>0){
                    try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                    System.out.println(getName()+"="+tickets--);
                }
                if (tickets<=0)break;
            }

        }
    }
}